﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using pwblog.Entities;
using pwblog.Common;

namespace pwblog.DataAccess
{
    public class CommentDao
    {
        private ManageBlogDataContext dataContext;

        public CommentDao()
        {
            dataContext = new ManageBlogDataContext();
        }

        /// <summary>
        /// insert 1 comment vao bang commnet
        /// </summary>
        /// <param name="cmt"></param>
        internal void InsertCommnet(Comment cmt)
        {
            dataContext.Comments.InsertOnSubmit(cmt);
            dataContext.SubmitChanges();
        }


        /// <summary>
        /// lay tat ca cac comment cua bai viet
        /// </summary>
        /// <returns></returns>
        internal Comment[] GetComments(int idArticle)
        {
            return dataContext.Comments.Where(item => item.IdArticle == idArticle).ToArray();
        }

        /// <summary>
        /// lay tat ca cac id Comment cua bai viet
        /// </summary>
        /// <param name="idArticle"></param>
        /// <returns></returns>
        internal Comment[] GetCommentsByIdArticle(int idArticle)
        {
            return dataContext.Comments.Where(item => item.IdArticle == idArticle).ToArray();
        }

        internal void DeleteComment(Comment comment)
        {
            dataContext.Comments.DeleteOnSubmit(comment);
            dataContext.SubmitChanges();
        }
        internal CommentWithArticle[] GetCommentsWithRange(int skip, int take)
        {
            //return dataContext.Comments.Skip(skip).Take(take).OrderBy(item => item.DateComment).ToArray();
            var result = from comm in dataContext.Comments
                         join arti in dataContext.Articles on comm.IdArticle equals arti.Id
                         select new CommentWithArticle { CommentId = comm.Id, Name = comm.Name, DateComment = comm.DateComment, Comment = comm.Comment1, ArticleTitle = arti.Name, ArticleId = comm.IdArticle };
            return result.ToArray();
        }
    }
}